
#import "FileUpload.h"

static NSString * const BOUNDRY        = @"--------------------------7d71a819230404";

@interface FileUpload (Private)

-(void)uploadSucceeded: (BOOL)success ;// IN

-(void)  upload ;

-(NSURLRequest *) createUrlRequest:(NSData *) imgData
						  UserName:(NSString*) userName
						  TextData:(NSString*) textData
					   ProductName:(NSString*) nameData
						 ProductID:(NSString*) proId;
@end

@implementation FileUpload(Private)

-(void)uploadSucceeded: (BOOL)success
{
    [delegate performSelector:success ? doneSelector : errorSelector  withObject:self];
}


-(void)connectionDidFinishLoading:(NSURLConnection *)connection  {
//    CMLog(@"connectionDidFinishLoading  %s: self:0x%p/n", __func__, self);
	
}



-(void)connection:(NSURLConnection *)connection    didFailWithError:(NSError *)error            {
 //   CMLog(@"didFailWithError %s: self:0x%p, connection error:%s/n", __func__, self, [[error description] UTF8String]);
	[self uploadSucceeded:NO];
}


-(void)connection:(NSURLConnection *)connection  
didReceiveResponse:(NSURLResponse *)response    {
//    CMLog(@"didReceiveResponse %s: self:0x%p/n", __func__, self);
}



-(void)connection:(NSURLConnection *)connection     
   didReceiveData:(NSData *)data              {
    NSLog(@"didReceiveData  %s: self:0x%p/n", __func__, self);
    NSString *reply  =[ [NSString alloc] initWithData:data
											 encoding:NSUTF8StringEncoding] ;
//    CMLog(@"%s: data: %s/n", __func__, [reply UTF8String]);
    if ([reply hasPrefix:@"YES"]) {
		[self uploadSucceeded:YES];
	}  else {
		[self   uploadSucceeded:NO];
		
	}
	
	[reply release];
}


//-----------END:  urlConnection Delegate --------

-(void) upload 
{
	NSURLRequest  * urlRequest =[self createUrlRequest: [self imageData]
											  UserName:  [self userName]
											  TextData:  [self commentText]
										   ProductName:  [self commentTitle]
											 ProductID:  [self commentId] ];
	
	NSURLConnection * connection =[
								   [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self] autorelease];
    if(!connection){
		
//		CMLog(@"CHUANG创建链接失败!");
	}
}

-(NSURLRequest *) createUrlRequest:(NSData *) imgData
						  UserName:(NSString*) nickName
						  TextData:(NSString*) textData
					   ProductName:(NSString*) nameData
						 ProductID:(NSString*) proId
{
	NSURL  * url=[NSURL  URLWithString: [self serverUrl]];
	NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url];
    [urlRequest setHTTPMethod:@"POST"];
    [urlRequest setValue: [NSString stringWithFormat:@"multipart/form-data; boundary=%@", BOUNDRY]
      forHTTPHeaderField:@"Content-Type"];
	
	int len=512;
	if(imgData !=nil){
		len = imgData.length + 512;
	}
	NSMutableData  * postData =[NSMutableData dataWithCapacity:len];
	[postData  appendData: [[NSString  stringWithFormat:@"--%@/r/n",BOUNDRY] dataUsingEncoding:NSUTF8StringEncoding]];
	[postData  appendData: [[NSString  stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"/r/n/r/n",@"productName" ] dataUsingEncoding:NSUTF8StringEncoding]];
	[postData  appendData: [[NSString  stringWithFormat:@"%@",nameData] dataUsingEncoding:NSUTF8StringEncoding]];
	[postData  appendData: [[NSString  stringWithFormat:@"/r/n--%@/r/n",BOUNDRY] dataUsingEncoding:NSUTF8StringEncoding]];
	[postData  appendData: [[NSString  stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"/r/n/r/n",@"productMessage" ] dataUsingEncoding:NSUTF8StringEncoding]];
	[postData  appendData:[[NSString   stringWithFormat:@"%@",textData] dataUsingEncoding:NSUTF8StringEncoding]];
	[postData  appendData:[[NSString   stringWithFormat:@"/r/n--%@/r/n",BOUNDRY] dataUsingEncoding:NSUTF8StringEncoding]];
	[postData  appendData: [[NSString  stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"/r/n/r/n",@"productId" ] dataUsingEncoding:NSUTF8StringEncoding]];
	[postData  appendData:[[NSString   stringWithFormat:@"%@",proId] dataUsingEncoding:NSUTF8StringEncoding]];
	[postData  appendData:[[NSString   stringWithFormat:@"/r/n--%@/r/n",BOUNDRY] dataUsingEncoding:NSUTF8StringEncoding]];
	[postData  appendData: [[NSString   stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"/r/n/r/n",@"userName" ] dataUsingEncoding:NSUTF8StringEncoding]];
	[postData  appendData: [[NSString   stringWithFormat:@"%@",nickName] dataUsingEncoding:NSUTF8StringEncoding]];
	if (imgData != nil ) {
		[postData  appendData:[[NSString   stringWithFormat:@"/r/n--%@/r/n",BOUNDRY] dataUsingEncoding:NSUTF8StringEncoding]];
		[postData  appendData: [[NSString  stringWithFormat:@"Content-Disposition: form-data; name=\"%@\";filename=\"temp.png\"/r/nContent-Type: image/png/r/n/r/n",@"productImage" ] dataUsingEncoding:NSUTF8StringEncoding]];
		[postData  appendData:imgData];
	}   
	//[postData  appendData:[@"Kkkk" dataUsingEncoding:NSUTF8StringEncoding]];
	[postData  appendData:[[NSString stringWithFormat:@"/r/n--%@--/r/n",BOUNDRY] dataUsingEncoding:NSUTF8StringEncoding]];
    [urlRequest setHTTPBody:postData];
    return urlRequest;
	
}

@end


@implementation FileUpload

@synthesize  serverUrl;
@synthesize userName;
@synthesize commentTitle;
@synthesize commentText;
@synthesize imageData ;
@synthesize commentId;


-(id)initWithUrlWithImageData: (NSString *)aServerUrl
					 userName:(NSString*) uName
				 commentTitle:(NSString*) title
				  commentText:(NSString*) msgText
					commentId:(NSString*) productId
					imageData:(NSData*) imgData
					 delegate: (id)aDelegate
				 doneSelector: (SEL)aDoneSelector
				errorSelector: (SEL)aErrorSelector 


{
	if(self==[super init]){
		[self setServerUrl:aServerUrl];
		delegate  = [aDelegate retain];
        doneSelector = aDoneSelector;
        errorSelector = aErrorSelector;
		[self setUserName:uName];
		[self setCommentId:productId];
		[self setCommentTitle:title];
		[self setCommentText:msgText];
		[self setImageData:imgData];
		
		[self upload];
		
	}
	return self;
}



-(void)dealloc
{
    [delegate release];
	delegate = nil;
	[userName release];
	userName=nil;
	[commentId release];
	commentId=nil;
	[commentText release];
	commentText=nil;
	[commentTitle release];
	commentTitle=nil;
	[serverUrl release];
	serverUrl =nil;
	[imageData release];
	imageData=nil;
    doneSelector = NULL;
    errorSelector = NULL;
    [super dealloc];
}
@end